package com.tqjc.execute.work.service.impl;

import com.tqjc.execute.utils.PlcOperator;
import com.tqjc.execute.utils.PublicResult;
import com.tqjc.execute.work.CraneWorkFactory;
import com.tqjc.execute.work.service.WorkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @projectName: BaotouSteel
 * @className: WorkServiceImpl
 * @author: Patienyon
 * @description: 工作服务具体实现
 * @date: 2023-07-03 9:14
 */

@Service
public class WorkServiceImpl implements WorkService {

    @Resource
    PlcOperator plcOperator;

    @Resource
    CraneWorkFactory craneWorkFactory;


    @Override
    public synchronized PublicResult checkIsReadyWork(String craneType) {
        Boolean nowLogo = craneWorkFactory.getCraneWorkImpl(craneType).getData().getStartLogo();
        if(nowLogo){
            return PublicResult.fail("当前正在执行自动任务，不可启动");
        }
        Boolean semiAutoIsRun = craneWorkFactory.getCraneWorkImpl(craneType).getData().getSemiAutoRunFlag();
        if(semiAutoIsRun){
            return PublicResult.fail("当前正在执行半自动任务，不可启动");
        }
        /*int semiAutoNowStep = craneWorkFactory.getCraneWorkImpl(craneType).getData().getSemiAutoNowStep();
        if(semiAutoNowStep!=0){
            return PublicResult.fail("当前半自动任务还未执行完最后步骤，不可启动");
        }*/
        int craneStatus = craneWorkFactory.getCraneWorkImpl(craneType).getData().getCraneStatus();
        if(craneStatus!=0){
            return PublicResult.fail("当前天车有故障，不可启动");
        }
        return PublicResult.ok("当前可以执行任务");
    }

    @Override
    public PublicResult checkSemiAutoIsReadyWork(String craneType) {
        Boolean nowLogo = craneWorkFactory.getCraneWorkImpl(craneType).getData().getStartLogo();
        if(nowLogo){
            return PublicResult.fail("当前正在执行自动任务，不可启动");
        }
        Boolean semiAutoRunFlag = craneWorkFactory.getCraneWorkImpl(craneType).getData().getSemiAutoRunFlag();
        if(semiAutoRunFlag){
            return PublicResult.fail("当前正在执行半自动任务子任务，不可下达指令");
        }
        return PublicResult.ok("当前可以执行半自动任务子任务");
    }

    @Override
    public PublicResult checkIsEmptyWork(String craneType) {
        Boolean nowLogo = craneWorkFactory.getCraneWorkImpl(craneType).getData().getStartLogo();
        if(!nowLogo){
            return PublicResult.fail("当前天车未启动，不需要停止任务");
        }
        return PublicResult.ok("当前1可以执行任务");
    }

}
